**********************************************
*******     Ethnic Diversity and Development:
*******         revisiting the evidence
*******           Montalvo and Reynal
*******               July, 2019
***********************************************

  cap restore
  clear all
  cap log close
  set more off
  set logtype smcl
  set matsize 11000
  set maxvar 30000


***** Working directory****


	* put here the full path where the data is located
	global main_path "C:\archivo\articulos\2015\ethnicity_and_scale\replication_files/"
	global tables "$main_path/"
	cd "$main_path"
	
	u "Montalvo_Reynal_2019_replication.dta", clear	
   
  global geo_vars "d1 rugged_slopeDG"
  global clim_var  "temp6080obs prec6080obs"
  global cont_tab2_c6 "$geo_vars d_coast $clim_var lpopden sharemin soilDG dis_river dis_lake latitude l_area_obs obspartitioned fracveg_all patho"

**************************************************************
** Table 1. Descriptive Stastics
**************************************************************
	
	eststo clear
	eststo: estpost tabstat grlight9210 llightnight1992capv2 FRACgregallv2  $cont_tab2_c6, stats(count mean sd p50 p90)   columns(statistics)
	
	
  cd "$tables"
  esttab using "$tables/table_1.tex" , cells("count(fmt(%9.0f))  mean(fmt(%9.3f)) sd(fmt(%9.3f)) p50(fmt(%9.3f)) p90(fmt(%9.3f)) ") replace fragment label nonum  unstack mlabels(none)  collabels(none) posthead("\\")
  
 
  
**************************************************************
** Table 2: Ethnic Diversity and Growth
**************************************************************   

  eststo clear

	*Column I
	eststo: xi: quietly regress  grlight9210 llightnight1992capv2 FRACgregallv2 i.country if dumpop==1, robust
  
	*Column II
	eststo: xi: quietly regress  grlight9210 llightnight1992capv2 FRACgregallv2  i.country  if dumpop==1,   cluster(country)
  
    *Column III
	eststo: xi: quietly regress  grlight9210 llightnight1992capv2 FRACgregallv2  $geo_vars   $clim_var dis_river dis_lake   i.country ,   cluster(country)

	*Column IV
	eststo: xi: quietly regress  grlight9210 llightnight1992capv2 FRACgregallv2  $geo_vars    $clim_var dis_river dis_lake lpopden  i.country ,   cluster(country)
  
	*Column V
	eststo: xi: quietly regress  grlight9210 llightnight1992capv2 FRACgregallv2 $geo_vars    $clim_var dis_river dis_lake lpopden sharemin soilDG  i.country ,   cluster(country)
  
	*Column VI
	eststo: xi: quietly regress  grlight9210 llightnight1992capv2 FRACgregallv2  $geo_vars   $clim_var  dis_river dis_lake  lpopden sharemin soilDG l_area_obs  i.country ,   cluster(country)
 
	*Column VII
	eststo: xi: quietly regress  grlight9210 llightnight1992capv2 FRACgregallv2  $geo_vars   $clim_var dis_river dis_lake lpopden sharemin soilDG  latitude l_area_obs obspartitioned i.country ,   cluster(country)
	
	*Column VIII
	eststo: xi: quietly regress  grlight9210 llightnight1992capv2 FRACgregallv2  $cont_tab2_c6 i.country ,   cluster(country)
	
	
	* Conley Standard Errors (you need to have ols_spatial_HAC ado)
	    *-- demeaning the data
     *gen time=0

     * xi i.country    
   	 * foreach i in grlight9210 llightnight1992capv2 FRACgregallv2   {
       * qui reg `i' _I* if pop90_sum!=0
       * cap drop fitlogy 
       * predict fitlogy if e(sample)
       * cap drop resid_`i'
       * gen resid_`i'  = fitlogy-`i'
     * }
		
		*** Controls
	* global cont_tab2_c6_resid ""
	* foreach i in $cont_tab2_c6   {
          * qui reg `i' _I* if pop90_sum!=0
          * cap drop fitlogy 
          * predict fitlogy if e(sample)
          * cap drop resid_`i'
          * gen resid_`i'  = fitlogy-`i'
		  * global cont_tab2_c6_resid "$cont_tab2_c6_resid resid_`i'"
    * }
		
    *    ols_spatial_HAC resid_grlight9210 resid_llightnight1992capv2 resid_FRACgregallv2 $cont_tab2_c6_resid, lat(latitude) lon(longitude) t(time) p(obs) dist(200) lag(0)
 
  
	* Export DataSet
	esttab using "$tables/table_2.tex", fragment star(* 0.1 ** 0.05 *** 0.01)  mlabels("(1)" "(2)" "(3)" "(4)" "(5)" "(6)" "(7)" "(8)") nonumber nomtitles replace brackets label se compress nogaps depvars se(%9.3f) b(%9.3f) stats(N r2, fmt(%9.0f  %9.3f) labels("Observations" "R-squared" ))  keep(llightnight1992capv2 FRACgregallv2 ) order( llightnight1992capv2 FRACgregallv2   )  indicate( "Geographic Variables= $geo_vars" "Climate Variables=$clim_var" "Population Density= lpopden"  "Share Mining and Fertile Soil= sharemin soilDG" "Distance to River and Lake= dis_river dis_lake" "Log. Area Obs= l_area_obs" "Country and Coastline Border=obspartitioned d_coast" "Dist. Equatorial Line= latitude" "Ecological Diversity= fracveg_all" "Pathogen stress= patho") 

*************************************************************************
** Table 3: Ethnic Diversity and Growth: the role of agglomeration
*************************************************************************  
  

  * Generating variables for next columns

  eststo clear
  * Column I
  eststo: xi: quietly regress  grlight9210 llightnight1992capv2 FRACgregallv2  nationalcapitals  $cont_tab2_c6  i.country ,   cluster(country)
  *Column II
  eststo: xi: quietly regress  grlight9210 llightnight1992capv2 FRACgregallv2  nationalcapitals provincialcapitals  $cont_tab2_c6  i.country ,   cluster(country)
  *Column III
  eststo: xi: quietly regress  grlight9210 llightnight1992capv2 FRACgregallv2  nationalcapitals provincialcapitals urbanagglomerations  $cont_tab2_c6 i.country ,   cluster(country)

  *Column IV: drop obs that have urban cities
  eststo: xi: quietly regress  grlight9210 llightnight1992capv2 FRACgregallv2  $cont_tab2_c6  i.country  if  nationalcapitals!=1 & provincialcapitals!=1 & urbanagglomerations!=1,   cluster(country)

  *Column V: drop the 10% richer
  eststo: xi: quietly regress  grlight9210 llightnight1992capv2 FRACgregallv2  nationalcapitals provincialcapitals urbanagglomerations  $cont_tab2_c6  i.country  if  rich<90,   cluster(country)

  *Column VI: drop 20% richer
  eststo: xi: quietly regress  grlight9210 llightnight1992capv2 FRACgregallv2  nationalcapitals provincialcapitals urbanagglomerations  $cont_tab2_c6  i.country  if  rich<80,   cluster(country)

  *Column VII: Drop 10% most densly pop
  eststo: xi: quietly regress  grlight9210 llightnight1992capv2 FRACgregallv2  nationalcapitals provincialcapitals   $cont_tab2_c6  i.country  if pop<90,   cluster(country)

  *Column VIII: Drop 20% most densly pop
  eststo: xi: quietly regress  grlight9210 llightnight1992capv2 FRACgregallv2  nationalcapitals provincialcapitals  $cont_tab2_c6  i.country  if pop<80,   cluster(country)

  * Export DataSet
    esttab using "$tables/table_3.tex", fragment star(* 0.1 ** 0.05 *** 0.01)  mlabels("(1)" "(2)" "(3)" "(4)" "(5)" "(6)" "(7)" "(8)") nonumber nomtitles replace brackets label se compress nogaps depvars se(%9.3f) b(%9.3f) stats(N r2, fmt(%9.0f  %9.3f) labels("Observations" "R-squared" ) layout("\num{@}"))  keep(llightnight1992capv2 FRACgregallv2  nationalcapitals provincialcapitals urbanagglomerations) order(llightnight1992capv2 FRACgregallv2  nationalcapitals provincialcapitals urbanagglomerations)  indicate( "Controls from Table 2= $cont_tab2_c6"  )  mgroups("" "" "" "Drop all-urban center" "Drop 10\% richest" "Drop 20\% richest" "Drop 10\% most densely" "Drop 20\% most densely", pattern(1 1 1 1 1 1 1 1))
	
************************************************************************************
** Table 4: Ethnic Diversity and Growth: Robustness to Alternative Data on Ethnicity
************************************************************************************

 
    eststo clear

     *Column I - (Column IV - Table 3)
    eststo: xi: quietly regress  grlight9210 llightnight1992capv2 FRACgregPOPall  $cont_tab2_c6  i.country ,   cluster(country)

    *Column II - (Column IV - Table 3)
    eststo: xi: quietly regress  grlight9210 llightnight1992capv2 fracethallv2  $cont_tab2_c6  i.country ,   cluster(country)

    *Column III - (Column IV - Table 3)
    eststo: xi: quietly regress  grlight9210 llightnight1992capv2 FRACmurdochobs  $cont_tab2_c6  i.country ,   cluster(country)


    *Column IV - Column V
    xtile n_frac=FRACgregallv2 , nq(100)
    eststo: xi: quietly regress  grlight9210 llightnight1992capv2 FRACgregallv2   $cont_tab2_c6  i.country if  n_frac<90,   cluster(country)

    esttab using "$tables/table_4.tex", fragment star(* 0.1 ** 0.05 *** 0.01)  mlabels("(1)" "(2)" "(3)" "(4)" "(5)") nonumber nomtitles replace brackets label se compress nogaps depvars se(%9.3f) b(%9.3f) stats(N r2, fmt(%9.0f  %9.3f) labels("Observations" "R-squared" ))  keep(llightnight1992capv2  FRACgregPOPall fracethallv2  FRACmurdochobs  FRACgregallv2  ) order(llightnight1992capv2  FRACgregPOPall fracethallv2 FRACmurdochobs FRACgregallv2 )  indicate(  "Controls from Table 2= $cont_tab2_c6") mgroups("" "" "" "Without Outliers FRAC" , pattern(1 1 1 1 1 1 1 1)) 

	
*********************************************************************************
** Table 5: Ethnic Diversity and Growth: the case of Africa
*********************************************************************************

preserve

  ********************************
  ***** Equatorial Line Analysis
  ********************************
  ** Gen
  gen lat_new=abs(latitude)

  gen d_20_eq=(lat_new<20)
  gen d_10_eq=(lat_new<10)
  gen d_5_eq=(lat_new<5)
  gen d_2_eq=(lat_new<2)

  cap lab var d_20_eq " +/- 20 degree from Equatorial Line  "
  cap lab var d_10_eq " +/- 10 degree from Equatorial Line  "
  cap lab var d_5_eq " +/- 5 degree from Equatorial Line  "
  cap lab var d_2_eq " +/- 2 degree from Equatorial Line  "

  foreach var in d_20_eq d_10_eq d_5_eq d_2_eq {
      cap gen int_`var'=`var'*FRACgregallv2
      loc varlabel = `"`: var label `var''"'
      lab var int_`var' " `varlabel' x FRACgregallv2"
  }
  *******************************************


   eststo clear

   
   *Column I
	eststo: xi: quietly regress  grlight9210 llightnight1992capv2 FRACgregallv2  $geo_vars   $clim_var    i.country if dummy_africa==1,   cluster(country)

	*Column II
	eststo: xi: quietly regress  grlight9210 llightnight1992capv2 FRACgregallv2  $geo_vars   $clim_var lpopden  i.country if dummy_africa==1,   cluster(country)
  
	*Column III
	eststo: xi: quietly regress  grlight9210 llightnight1992capv2 FRACgregallv2 $geo_vars    $clim_var lpopden sharemin soilDG  i.country if dummy_africa==1,   cluster(country)
  
	*Column IV
	eststo: xi: quietly regress  grlight9210 llightnight1992capv2 FRACgregallv2  $geo_vars     $clim_var     lpopden sharemin soilDG dis_river dis_lake  i.country if dummy_africa==1,   cluster(country)
 
	*Column V
	eststo: xi: quietly regress  grlight9210 llightnight1992capv2 FRACgregallv2  $geo_vars d_coast $clim_var lpopden sharemin soilDG dis_river dis_lake latitude l_area_obs obspartitioned i.country if dummy_africa==1,   cluster(country)
	
	*Column VI
	eststo: xi: quietly regress  grlight9210 llightnight1992capv2 FRACgregallv2  $cont_tab2_c6 i.country if dummy_africa==1,   cluster(country)
	
	* Column VII and VIII
    foreach i in d_10_eq d_5_eq {
      eststo: xi: quietly regress  grlight9210 llightnight1992capv2 FRACgregallv2 `i' int_`i' $cont_tab2_c6  i.country if dummy_africa==1,   cluster(country)
    }

 	* Export DataSet
	esttab using "$tables/table_5.tex", fragment star(* 0.1 ** 0.0599 *** 0.01)  mlabels("(1)" "(2)" "(3)" "(4)" "(5)" "(6)" "(7)" "(8)") nonumber nomtitles replace brackets label se compress nogaps depvars se(%9.3f) b(%9.3f) stats(N r2, fmt(%9.0f  %9.3f) labels("Observations" "R-squared" ))  keep(llightnight1992capv2 FRACgregallv2 d_10_eq d_5_eq  int_d_10_eq int_d_5_eq) order(llightnight1992capv2 FRACgregallv2)  indicate( "Geographic Variables= $geo_vars" "Climate Variables=$clim_var" "Population Density= lpopden"  "Share Mining and Fertile Soil= sharemin soilDG" "Distance to River and Lake= dis_river dis_lake" "Log. Area Obs= l_area_obs" "Country and Coastline Border=obspartitioned d_coast" "Dist. Equatorial Line= latitude" "Ecological Diversity= fracveg_all" "Pathogen stress= patho")  mgroups("" "" "" "" "" "" "" "" , pattern(1 1 1 1 1 1 1 1)) 




